package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/permission")
public class PermissionServlet extends BaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置页码和页数
        int currentPage = 1;
        int pageSize = 5;
        if (StringUtils.isNotBlank(request.getParameter("currentPage"))) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = permissionService.findAll(currentPage, pageSize);

        //将数据保存到指定的位置
        request.setAttribute("page", all);

        //请求转发
        request.getRequestDispatcher("/pages/system/permission/permission-list.jsp").forward(request, response);
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //加载所有的部门信息放入到permissionList
        List<Permission> all = permissionService.findAll();
        request.setAttribute("permissionList", all);

        //跳转页面
        request.getRequestDispatcher("/pages/system/permission/permission-add.jsp").forward(request, response);

       /* //记日志
        SysLogServlet.logMethodForServlet(request);*/
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        //调用业务层接口save

        //System.out.println(permission);
        permissionService.save(permission);
        //跳转回到页面list

        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");

       /* //记日志
        SysLogServlet.logMethodForServlet(request);*/
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
//        Permission permission = BeanUtil.fillBean(request,Permission.class);
        String id = request.getParameter("id");
        //调用业务层接口save
//        PermissionService permissionService = new PermissionServiceImpl();
        permissionService.delete(id);

        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");

        /*//记日志
        SysLogServlet.logMethodForServlet(request);*/


    }

    // 删除多个复选框
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取要删除的元素id数组
        String[] ids = request.getParameterValues("ids");
        for (String id : ids) {
            permissionService.deleteByIds(id.split(","));
        }

        //重定向查询所有
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=list");


    }
    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置页码和页数
        int currentPage = 1;
        int pageSize = 5;
        if (StringUtils.isNotBlank(request.getParameter("currentPage"))) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        String sorting_desc = request.getParameter("sorting_desc");
        System.out.println("sorting_desc = " + sorting_desc);

        PageInfo all = permissionService.search(sorting_desc, currentPage, pageSize);
        request.setAttribute("page", all);
        //请求转发
        request.getRequestDispatcher("/pages/system/permission/permission-list.jsp").forward(request, response);
    }

    public void addAuthor(HttpServletRequest request, HttpServletResponse response){
        try {
        String[] ids = request.getParameterValues("ids");
        String roleId = request.getParameter("roleId");
        System.out.println("roleId = " + roleId);
        for (String id : ids) {
            System.out.println("id = " + id);
        }
        //根据roleID删除关系
        permissionService.deleteAuthorByRoleId(roleId);
        //重新增加关系
        permissionService.addNewAuthor(ids,roleId);

        response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}



